﻿using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TZReport_Util.Basic;
using TZReport_Util.Log;

namespace TZReport_Util.Util
{
    public class QuartzHelper
    {
        private static IScheduler scheduler;

        static QuartzHelper()
        {
            scheduler = StdSchedulerFactory.GetDefaultScheduler();
        }

        private const string InitRecord = "InitRecord";
        private const string UploadData = "UploadData";

        /// <summary>
        /// 启动开始
        /// </summary>
        public static void StartScheduler()
        {
            IJobDetail jobDetail_1 = new JobDetailImpl(InitRecord, typeof(InitRecord));
            IJobDetail jobDetail_2 = new JobDetailImpl(UploadData, typeof(UploadData));

            var trigger_1 = TriggerBuilder.Create().WithIdentity(new TriggerKey(InitRecord, InitRecord)).WithSimpleSchedule(a => a.WithIntervalInSeconds(ConfigSource.initSeconds).RepeatForever()).Build();
            var trigger_2 = TriggerBuilder.Create().WithIdentity(new TriggerKey(UploadData, UploadData)).WithSimpleSchedule(a => a.WithIntervalInSeconds(ConfigSource.uploadSeconds).RepeatForever()).Build();

            Dictionary<IJobDetail, Quartz.Collection.ISet<ITrigger>> dicJob = new Dictionary<IJobDetail, Quartz.Collection.ISet<ITrigger>>();
            Quartz.Collection.ISet<ITrigger> triggers_1 = new Quartz.Collection.HashSet<ITrigger>();
            triggers_1.Add(trigger_1);


            Quartz.Collection.ISet<ITrigger> triggers_2 = new Quartz.Collection.HashSet<ITrigger>();
            triggers_2.Add(trigger_2); 

            dicJob.Add(jobDetail_1, triggers_1);
            dicJob.Add(jobDetail_2, triggers_2);

            scheduler.ScheduleJobs(dicJob, true);

            scheduler.Start();
        }


        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="strs"></param>
        public static void PauseTrigger()
        {
            // 正常时暂停
            if (GetTriggerState(InitRecord, InitRecord) == 0)
                scheduler.PauseTrigger(new TriggerKey(InitRecord, InitRecord));

            if (GetTriggerState(UploadData, UploadData) == 0)
                scheduler.PauseTrigger(new TriggerKey(UploadData, UploadData));
        }

        /// <summary>
        /// 恢复
        /// </summary>
        public static void ResumeTrigger()
        {
            // 暂停时恢复
            if (GetTriggerState(InitRecord, InitRecord) == 1)
                scheduler.ResumeTrigger(new TriggerKey(InitRecord, InitRecord));

            if (GetTriggerState(UploadData, UploadData) == 1)
                scheduler.ResumeTrigger(new TriggerKey(UploadData, UploadData));
        }

        public static int GetTriggerState(string name, string group)
        {
            return (int)scheduler.GetTriggerState(new TriggerKey(name, group));
        }
    }

    public class InitRecord : IJob
    {
        private LogHelper logHelper = new LogHelper(typeof(InitRecord));
        private WebUtils webUtils = new WebUtils();
        public void Execute(IJobExecutionContext context)
        {
            string url = ConfigSource.initUrl;
            Debug.WriteLine(url);
            string result = webUtils.DoGet(url, "utf-8");
            logHelper.Info(result);
        }
    }


    /// <summary>
    /// 初始化pdf上传的数据
    /// </summary>
    public class UploadData : IJob
    {
        private LogHelper logHelper = new LogHelper(typeof(UploadData));
        private WebUtils webUtils = new WebUtils();
        public void Execute(IJobExecutionContext context)
        {
            string url = ConfigSource.uploadUrl;
            Debug.WriteLine(url);
            string result = webUtils.DoGet(url, "utf-8");
            logHelper.Info(result);
        }
    }
}
